﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ArchitectingInnovation.PRISMvvM.Core.Extensions
{
    public class BindingScope
    {
        public string ElementName { get; set; }

        public string PropertyName { get; set; }

        public void Bind(FrameworkElement frameworkElement)
        {
            
            var fields = frameworkElement.GetType().GetFields(
                System.Reflection.BindingFlags.Public |
                System.Reflection.BindingFlags.FlattenHierarchy |
                System.Reflection.BindingFlags.Static);
            foreach (var field in fields)
            {
                if (field.FieldType == typeof(DependencyProperty) &&
                    (field.Name == PropertyName ||
                    field.Name == string.Concat(PropertyName, "Property")))
                {
                    DependencyProperty dp = field.GetValue(frameworkElement) as DependencyProperty;
                    var binding = frameworkElement.GetBindingExpression(dp);
                    string elementName = binding.ParentBinding.ElementName;
                    if (!string.IsNullOrEmpty(elementName))
                    {
                        DependencyObject boundToElement = GetElementBasedOnName(frameworkElement, elementName);
                        if (boundToElement != null)
                        {
                            Binding newBinding = new Binding();
                            Binding oldBinding = binding.ParentBinding;
                            newBinding.BindsDirectlyToSource = oldBinding.BindsDirectlyToSource;
                            newBinding.Converter = oldBinding.Converter;
                            newBinding.ConverterCulture = oldBinding.ConverterCulture;
                            newBinding.ConverterParameter = oldBinding.ConverterParameter;
                            newBinding.FallbackValue = oldBinding.FallbackValue;
                            newBinding.Mode = oldBinding.Mode;
                            newBinding.NotifyOnValidationError = oldBinding.NotifyOnValidationError;
                            newBinding.Path = oldBinding.Path;
                            newBinding.Source = boundToElement;
                            newBinding.StringFormat = oldBinding.StringFormat;
                            newBinding.TargetNullValue = oldBinding.TargetNullValue;
                            newBinding.UpdateSourceTrigger = oldBinding.UpdateSourceTrigger;
                            newBinding.ValidatesOnDataErrors = oldBinding.ValidatesOnDataErrors;
                            newBinding.ValidatesOnExceptions = oldBinding.ValidatesOnExceptions;
                            newBinding.ValidatesOnNotifyDataErrors = oldBinding.ValidatesOnNotifyDataErrors;
                            if (frameworkElement is ComboBox)
                            {
                                ComboBox combo = frameworkElement as ComboBox;
                                combo.SetBinding(dp, newBinding);
                                if (null != combo.GetBindingExpression(ComboBox.SelectedItemProperty))
                                    combo.SetBinding(ComboBox.SelectedItemProperty,
                                                        combo.GetBindingExpression(ComboBox.SelectedItemProperty).
                                                            ParentBinding);
                            }
                            else
                            {
                                frameworkElement.SetBinding(dp, newBinding);
                            }

                        }
                    }
                }
            }
            
        }

        private DependencyObject GetElementBasedOnName(DependencyObject startPoint, string elementName)
        {
            DependencyObject returnValue = null;
            if (startPoint != null)
            {
                DependencyObject parent = VisualTreeHelper.GetParent(startPoint);
                if (parent != null)
                {
                    FrameworkElement fe = parent as FrameworkElement;
                    if (fe != null)
                    {
                        if (fe.Name == elementName)
                        {
                            returnValue = fe;
                        }
                        else
                        {
                            returnValue = GetElementBasedOnName(fe, elementName);
                        }
                    }
                    else
                    {
                        returnValue = GetElementBasedOnName(fe, elementName);
                    }
                }
            }
            return returnValue;
        }
    }
}
